'use strict';

const Service = require('egg').Service;
// 传递currentPage 和 pageSize  返回的东西有 count总数 ，limit 每页条数，data 切片的数据 
class BaseService extends Service {
  // 返回查询时传递的参数 offset 偏移值，limt 条数 
  pageParam (params) {
    const page = this.getCurrentPage(params.currentPage); //当前页码
    let limit = 20;
    if (params.pageSize && parseInt(params.pageSize) > 0 && parseInt(params.pageSize) <= 100) {
      limit = parseInt(params.pageSize); 
    }
    console.log(limit);
    return {
      offset: (page - 1) * limit,  
      limit,    
    };
  }

  // 返回当前页数
  getCurrentPage (currentPage) {
    let page = 1;
    if (currentPage && parseInt(currentPage) > 0) {
      page = parseInt(currentPage);
    }
    return page;
  }

  paginate (count, rows,params) {
    // const { limit } = this.pageParam(params);  
    return {
      total: count,  
      // per_page: limit, 
      // last_page: count % limit > 0 ? parseInt(count / limit + '') + 1 : parseInt(count / limit + ''),
      data: rows,
    };
  }

  countAllPaginate (data) {
    const { count, rows } = data;
    return this.paginate(count, rows);
  }
}

module.exports = BaseService;
